package org.tangdao.modules.sys.model.domain;

import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.tangdao.common.collect.ListUtils;
import org.tangdao.common.config.Global;
import org.tangdao.common.mapper.JsonMapper;
import org.tangdao.common.suports.DataEntity;
import org.tangdao.modules.sys.model.vo.RoleDataScope;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 角色表
 * </p>
 *
 * @author ruyang
 * @since 2019-07-02
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_role")
public class Role extends DataEntity<Role> {

	private static final long serialVersionUID = 1L;

	/**
	 * 角色编码
	 */
	@TableId
	@NotNull(message = "角色编码不能为空")
	private String roleCode;

	/**
	 * 角色名称
	 */
	@NotNull(message = "角色名称不能为空")
	private String roleName;

	/**
	 * 角色排序（升序）
	 */
	@NotNull(message = "角色排序不能为空")
	private Integer roleSort;

	/**
	 * 数据范围设置（0未设置 1全部数据 2自定义数据）
	 */
	private String dataScope;

	/**
	 * 用户类型
	 */
	private String userType;

	@JsonIgnore
	@TableField(exist = false)
	private List<RoleDataScope> roleDataScopeList = ListUtils.newArrayList();

	// 默认管理员角色
	public static final String DEFAULT_ADMIN_ROLE_CODE = Global.getProperty("user.defaultAdminRoleCode", "corpAdmin");

	public static final String DATA_SCOPE_NONE = "0";
	public static final String DATA_SCOPE_ALL = "1";
	public static final String DATA_SCOPE_CUSTOM = "2";

	@TableField(exist = false)
	private String userCode;

	public void setRoleDataScopeListJson(String jsonString) {
		List<Map<String, Object>> list = JsonMapper.fromJson(jsonString, List.class);
		if (list != null) {
			for (Map<String, Object> map : list) {
				RoleDataScope roleDataScope = new RoleDataScope();
				roleDataScope.setRoleCode(this.roleCode);
				roleDataScope.setCtrlType((String) map.get("ctrlType"));
				roleDataScope.setCtrlData((String) map.get("ctrlData"));
				roleDataScope.setCtrlPermi(DATA_SCOPE_ALL);
				this.roleDataScopeList.add(roleDataScope);
			}
		}
	}
}
